#include "CheckBox.h"
#include "Scene.h"

using namespace game_engine;
using namespace game_engine::gui;
using namespace game_engine::components;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

CheckBox::CheckBox(void)
    : AbstractButton(), _uncheckedTexture(NULL), _checkedTexture(NULL)
{
}

CheckBox::CheckBox(Scene *scene, const string &name, const string &tag, 
                   const DrawLevelType drawLevel)
    : AbstractButton(scene, name, tag, drawLevel)
{
    setCheckable(true);
    setChecked(true);
    _uncheckedTexture = new PlainTexture(this, "unchecked.png", GL_RGBA, true);
    _checkedTexture = new PlainTexture(this, "checked.png", GL_RGBA, true);

    // Initialize textures at texture manager level
    _uncheckedTexture->initialize();
    _checkedTexture->initialize();

    //addComponent(_uncheckedTexture);
    addComponent(_checkedTexture);
}

CheckBox::~CheckBox(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void CheckBox::update(const FLOAT32 time)
{
    BOOL checkedState = isChecked();

    // Call method from parent
    AbstractButton::update(time);

    // Set the appropriate texture if checked state has changed
    BOOL newCheckedState = isChecked();
    if (checkedState != newCheckedState)
    {
        if (isChecked())
        {
            if (hasComponent("PlainTexture"))
            {
                removeComponent("PlainTexture");
            }
            addComponent(_checkedTexture);
        }
        else
        {
            if (hasComponent("PlainTexture"))
            {
                removeComponent("PlainTexture");
            }
            addComponent(_uncheckedTexture);
        }
    }
}
